package com.china08.yxyapi.api.comm;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;

@CrossOrigin
@RestController
public class KnowResourceCountController {

    @Autowired
    private YxyQuesKnowRepository YxyQuesKnowRepository;

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private ElasticsearchTemplate et;

    @RequestMapping(value = "/api_yw/comm/know/resourcecount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(@RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();

        // 微课数量
        BoolQueryBuilder weikeBoolQueryBuilder = new BoolQueryBuilder();
        BoolQueryBuilder knowBoolQueryBuilder = new BoolQueryBuilder();
        TermQueryBuilder knowBuilder1 = new TermQueryBuilder("knowIds", reqModel.knowId);
        TermQueryBuilder knowBuilder2 = new TermQueryBuilder("knowOneLevelIds", reqModel.knowId);
        TermQueryBuilder knowBuilder3 = new TermQueryBuilder("knowTwoLevelIds", reqModel.knowId);
        knowBoolQueryBuilder.should(knowBuilder1);
        knowBoolQueryBuilder.should(knowBuilder2);
        knowBoolQueryBuilder.should(knowBuilder3);
        weikeBoolQueryBuilder.must(knowBoolQueryBuilder);
        weikeBoolQueryBuilder.must(new TermQueryBuilder("isDelete", 0));
        SearchQuery query = new NativeSearchQueryBuilder().withQuery(weikeBoolQueryBuilder).build();
        long weikeCount = et.count(query, ElasticYxyResResource.class);
        respModel.weikeCount = weikeCount;

        // 试题数量
        SearchQuery quesQuery = new NativeSearchQueryBuilder()
                .withQuery(new BoolQueryBuilder().must(new TermQueryBuilder("knowFullIds", reqModel.knowId))).build();
        long quesCount = et.count(quesQuery, ElasticYxyYxyQuesResource.class);
        respModel.quesCount = quesCount;

        List<YxyQuesKnow> yxyQuesKnow = new ArrayList<YxyQuesKnow>();
        yxyQuesKnow = getChildren(reqModel.knowId, yxyQuesKnow);
        List<Integer> allKnowIds = yxyQuesKnow.stream().map(x -> x.getId()).collect(Collectors.toList());
        // 资源数量
        List<String> bookcontents = new ArrayList<String>();
        if (allKnowIds.size() > 0) {
            bookcontents = yxyResBookContentsKnoledgesRepository.getBookcontentByKnowIdIn(allKnowIds);
        }
        if (bookcontents.size() > 0) {
            BoolQueryBuilder resourceBoolQueryBuilder = new BoolQueryBuilder();
            resourceBoolQueryBuilder.must(new TermQueryBuilder("isDelete", 0));
            resourceBoolQueryBuilder.must(new TermsQueryBuilder("bookContentIdList", bookcontents));
            SearchQuery resourceQuery = new NativeSearchQueryBuilder().withQuery(resourceBoolQueryBuilder).build();
            long resourceCount = et.count(resourceQuery, ElasticYxyResResource.class);
            respModel.resourceCount = resourceCount;
        }
        return respModel;
    }

    public List<YxyQuesKnow> getChildren(Integer knowId, List<YxyQuesKnow> knowledges) {
        YxyQuesKnow knowledge = YxyQuesKnowRepository.findById(knowId);
        if (knowledge != null) {
            knowledges.add(knowledge);
        }
        List<YxyQuesKnow> childKnowledges = YxyQuesKnowRepository.findByParentId(knowId);
        knowledges.addAll(childKnowledges);
        List<Integer> knowIds = childKnowledges.stream().map(x -> x.getId()).collect(Collectors.toList());
        if (knowIds.size() > 0) {
            List<YxyQuesKnow> childKnowledges2 = YxyQuesKnowRepository.findByParentIdIn(knowIds);
            knowledges.addAll(childKnowledges2);
        }
        return knowledges;
    }

    protected static class ReqModel {
        public Integer knowId;
    }

    protected static class RespModel extends YxyApiReqModel {
        public long resourceCount = 0;
        public long weikeCount = 0;
        public long quesCount;
    }

}
